<?php

namespace backend\modules\system\controllers;

use backend\extensions\ApiBaseController;
use backend\services\captcha\CaptchaService;
use backend\services\system\AdminService;

class AdminController extends ApiBaseController {
    
    /**
     * 图形验证码
     * **/
   public function actionCaptcha() {
       $data = [
            'base64' => CaptchaService::getCaptcha()
        ];
        return $this->jsonSuccess($data);
    }

    /**
     * 后台登录
     * * */
    public function actionLogin() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['account', 'password','code'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return AdminService::login($this->params);
    }
    
    /**
     * 获取权限
     * **/
    public function actionGetMenu(){
        return AdminService::getMenu();
    }
    
    
    /**
     * 列表
     * * */
    public function actionList() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['name','phone'], 'default', 'value' => 0, 'message' => '检索条件'],
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
            [['page_size'], 'default', 'value' => 30, 'message' => '每页显示总数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = AdminService::getList($this->params);
        return $this->jsonSuccess($data);
    }
    
    /**
     * 新增
     * * */
    public function actionCreate() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['account','realname','phone','status','sex'], 'required', 'message' => '{attribute}属必填项'],
            [['id','desc','role_id','password'], 'default', 'value' => '', 'message' => '非必填参数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        if($this->params['id']){
            return AdminService::update($this->params);
        }
        unset($this->params['id']);
        return AdminService::create($this->params);
    }
    
    /**
     * 删除用户
     * * */
    public function actionDel() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return AdminService::delete($this->params);
    }
    
    /**
     * 更新状态
     * * */
    public function actionStatus() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id','status'], 'required', 'message' => '{attribute}属必填项'],
            [['status'], 'in', 'range' => [1, 2], 'message' => '{attribute}范围1~2'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return AdminService::update($this->params);
    }
    
    /**
     * 导出
     * **/
    public function actionExport(){
        $params = \Yii::$app->request->get();
        $rules = [
            [['name','phone'], 'default', 'value' => 0, 'message' => '检索条件'],
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
            [['page_size'], 'default', 'value' => 100000, 'message' => '每页显示总数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = AdminService::getList($this->params);
        return AdminService::Export($data['list']);
    }
    
    /**
     * 导出
     * **/
    public function actionImport(){
        return AdminService::Import(\yii\web\UploadedFile::getInstanceByName('file'));
    }

}
